<?php


namespace app\admin\controller;


use app\BaseController;
use app\service\roominfoService;
use think\facade\Db;
use think\facade\View;

class ZftOrder extends Common
{
    //直付通账单
    public function index(){
        $com = $this->pidTokenId();
        //$whe['community_id'] = array('in',$com['community_id']);
        $where[]=["community_id","in",$com["com_id"]];
        $communitylist = (new roominfoService())->communityList($where);
        View::assign('communitylist',$communitylist);

        $data = request()->get();
        //var_dump($data);
        if (!empty($data)){
            if(!empty($data['community_name'])){
                $where[] = ["community_name","like","%".$data['community_name']."%"];
            }
            if(!empty($data["start_time"])&&(empty($data["end_time"]))){
                //$start_time=strtotime($data["start_time"])*1000;
                $start_time=$data["start_time"];
                $where[] = ['pay_time','>=',$start_time];
            }else if(empty($data["start_time"])&&(!empty($data["end_time"]))){
                $end_time=strtotime($data["end_time"])*1000;
                $end_time=$data["end_time"];
                $where[] = ['pay_time','<=',$end_time];
            }else if(!empty($data["start_time"])&&(!empty($data["end_time"]))){
                //$start_time=strtotime($data["start_time"])*1000;
                //$end_time=strtotime($data["end_time"])*1000;
                $start_time=$data["start_time"];
                $end_time=$data["end_time"];
                $where[] = ['pay_time','>=',$start_time];
                $where[] = ['pay_time','<=',$end_time];
            }

            View::assign('where',$where);
            $where = array_filter($where);


        }
        //var_dump($where);

        $page = request()->has('page') ? request()->get('page') : 1;

        $export = $where;

        $export = json_encode($export);
        $export = str_replace("\"","|",$export);
        View::assign('export',$export);
        $list=Db::name('zft_order_success')
            ->where($where)
            ->order('uid','desc')
            ->paginate([
                'list_rows'=> 10,
                'page'  =>  $page,
                'query' =>  $where
            ]);
        //var_dump($list);
        $number=Db::name('zft_order_success')->where($where)->count();
        View::assign("number",$number);
        View::assign("list",$list);
        foreach ($data as $k => $v) {
            View::assign($k, $v);
        }
        return View::fetch("index");
    }
    //直付通结算账单
    public function settle(){
        //var_dump(env("APP.royalty_trans_in"));
        $com = $this->pidTokenId();
        //$whe['community_id'] = array('in',$com['community_id']);
        $where[]=["community_id","in",$com["com_id"]];
        $communitylist = (new roominfoService())->communityList($where);
        View::assign('communitylist',$communitylist);

        $data = request()->get();
        //var_dump($data);
        if (!empty($data)){
            if(!empty($data['community_name'])){
                $where[] = ["community_name","like","%".$data['community_name']."%"];
            }
            if(!empty($data["start_time"])&&(empty($data["end_time"]))){
                //$start_time=strtotime($data["start_time"])*1000;
                $start_time=$data["start_time"];
                $where[] = ['ctime','>=',$start_time];
            }else if(empty($data["start_time"])&&(!empty($data["end_time"]))){
                $end_time=strtotime($data["end_time"])*1000;
                $end_time=$data["end_time"];
                $where[] = ['ctime','<=',$end_time];
            }else if(!empty($data["start_time"])&&(!empty($data["end_time"]))){
                //$start_time=strtotime($data["start_time"])*1000;
                //$end_time=strtotime($data["end_time"])*1000;
                $start_time=$data["start_time"];
                $end_time=$data["end_time"];
                $where[] = ['ctime','>=',$start_time];
                $where[] = ['ctime','<=',$end_time];
            }

            View::assign('where',$where);
            $where = array_filter($where);


        }
        //var_dump($where);

        $page = request()->has('page') ? request()->get('page') : 1;

        $export = $where;

        $export = json_encode($export);
        $export = str_replace("\"","|",$export);
        View::assign('export',$export);
        $list=Db::name('zft_settle_order')
            ->where($where)
            ->order('id','desc')
            ->paginate([
                'list_rows'=> 10,
                'page'  =>  $page,
                'query' =>  $where
            ]);
        //var_dump($list);
        $number=Db::name('zft_settle_order')->where($where)->count();
        View::assign("number",$number);
        if($number>0){

        }
        $order_money=Db::name('zft_settle_order')->where($where)->sum('order_money');
        $settle_money=Db::name('zft_settle_order')->where($where)->sum('settle_money');
        View::assign("order_money",$order_money);
        View::assign("settle_money",$settle_money);
        View::assign("list",$list);
        foreach ($data as $k => $v) {
            View::assign($k, $v);
        }
        return View::fetch("settle");
    }

    public function statistics(){
        $com = $this->pidTokenId();
        //$whe['community_id'] = array('in',$com['community_id']);
        $where[]=["community_id","in",$com["com_id"]];
        $communitylist = (new roominfoService())->communityList($where);
        View::assign('communitylist',$communitylist);

        $data = request()->get();
        //var_dump($data);
        if (!empty($data)){
            if(!empty($data['community_name'])){
                $where[] = ["community_name","like","%".$data['community_name']."%"];
            }
            if(!empty($data["start_time"])&&(empty($data["end_time"]))){
                //$start_time=strtotime($data["start_time"])*1000;
                $start_time=$data["start_time"];
                $where[] = ['pay_time','>=',$start_time];
            }else if(empty($data["start_time"])&&(!empty($data["end_time"]))){
                $end_time=strtotime($data["end_time"])*1000;
                $end_time=$data["end_time"];
                $where[] = ['pay_time','<=',$end_time];
            }else if(!empty($data["start_time"])&&(!empty($data["end_time"]))){
                //$start_time=strtotime($data["start_time"])*1000;
                //$end_time=strtotime($data["end_time"])*1000;
                $start_time=$data["start_time"];
                $end_time=$data["end_time"];
                $where[] = ['pay_time','>=',$start_time];
                $where[] = ['pay_time','<=',$end_time];
            }

            View::assign('where',$where);
            $where = array_filter($where);


        }
        //var_dump($where);

        $page = request()->has('page') ? request()->get('page') : 1;

        $export = $where;

        $export = json_encode($export);
        $export = str_replace("\"","|",$export);
        View::assign('export',$export);
        $list=Db::name('zft_statistics_day')
            ->where($where)
            ->order('id','desc')
            ->paginate([
                'list_rows'=> 10,
                'page'  =>  $page,
                'query' =>  $where
            ]);
        //var_dump($list);
        $number=Db::name('zft_statistics_day')->where($where)->count();
        View::assign("number",$number);

        View::assign("list",$list);
        foreach ($data as $k => $v) {
            View::assign($k, $v);
        }
        return View::fetch("statistics");
    }
    //执行回调操作
    public function doNotify(){
        //$id=rq
    }

}